package chat_utils

import (
	"fmt"
	"hft/Database/admin"
	"hft/Http/Services/front/chat/chat_model"
	"strings"
	"time"

	"gorm.io/gorm"
)

// 转换时间
func TransTime(myTime int) string {
	if myTime <= 0 {
		return "暂无资料"
	}
	timeStamp := int64(myTime)
	t := time.Unix(timeStamp/1000, 0)
	formattedDate := t.Format("2006年01月02日")
	return formattedDate
}

// 暂无资料
func TransString(str string) string {
	if str == "" {
		return "暂无资料"
	}
	return str
}

// 获取户型
func GetRoomType(db *gorm.DB, louID int, roomTypes []*admin.RoomType) string {
	var ids []int
	for _, v := range roomTypes {
		ids = append(ids, v.Code)
	}
	var rooms []*admin.Room
	db.Where("room_id IN ? AND lou_pan_id = ?", ids, louID).Find(&rooms)
	var roomListName = GetCompositionRoom(rooms, ids)
	return roomListName
}

// 获取户型组装
func GetCompositionRoom(rooms []*admin.Room, ids []int) string {
	var list []string
	for _, v := range ids {
		var item = new(chat_model.RoomItem)
		item.Num = 0
		item.Name = TransRoomName(v)
		for _, j := range rooms {
			if v == j.RoomId {
				item.Num++
			}
		}
		if item.Num != 0 {
			list = append(list, fmt.Sprintf("%v(%v)", item.Name, item.Num))
		}
	}
	return strings.Join(list, "，")
}

// 判断户型名称
func TransRoomName(code int) string {
	if code == 1 {
		return "一室"
	} else if code == 2 {
		return "二室"
	} else if code == 3 {
		return "三室"
	} else if code == 4 {
		return "四室"
	} else if code >= 5 {
		return "五室以上"
	} else {
		return ""
	}
}

// 获取地址
func GetBelongAddress(belongName string, address string) string {
	var belongArr = strings.Split(belongName, "-")
	if len(belongArr) == 3 {
		return fmt.Sprintf("[%v-%v]%v", belongArr[len(belongArr)-1], belongArr[len(belongArr)-1], address)
	} else {
		return fmt.Sprintf("[%v-%v]%v", belongArr[len(belongArr)-2], belongArr[len(belongArr)-1], address)
	}
}

// 获取secondLine
func GetSecondLine(belongName string, min_area int, max_area int) string {
	if min_area == 0 && max_area == 0 {
		return "暂无资料"
	}
	var area string
	if min_area == max_area && min_area != 0 {
		area = fmt.Sprintf("建面%vm²", min_area)
	} else {
		area = fmt.Sprintf("建面%v-%vm²", min_area, max_area)
	}
	var belongArr = strings.Split(belongName, "-")
	return fmt.Sprintf("%v %v", belongArr[2], area)
}
